package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.common.InviteRequestUtil;
import com.aier.classes.entity.InviteTeacher;
import com.aier.common.common.SystemDateFormatUtil;


public class InviteTeacherRowMapper implements RowMapper<InviteTeacher> {

    private int type;

    public static final int AND_INVITETEACHER = 1;
    
    public static final int AND_GNAME = 2;
    
    public InviteTeacherRowMapper() {
    }

    public InviteTeacherRowMapper(int type) {
        this.type = type;
    }

    @Override
    public InviteTeacher mapRow(ResultSet rs, int rowNum) throws SQLException {
        InviteTeacher it = parseInviteTeacher(new InviteTeacher(), rs);
        it.setCtime_str(SystemDateFormatUtil.formatDate(it.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
        it.setState_str(InviteRequestUtil.InviteStateMap.get(it.getState()));
        
        switch (type) {
            case AND_INVITETEACHER:
                it.setName_teacher(rs.getString("name_teacher"));
                it.setName_school(rs.getString("name_school"));
                it.setName_class(rs.getString("name_class"));
                break;
            case AND_GNAME:
                it.setGname(rs.getString("gname"));
                it.setName_class(rs.getString("cname"));
                break;
        }
        
        return it;
    }

    public static InviteTeacher parseInviteTeacher(InviteTeacher s, ResultSet rs) throws SQLException {
        return parseInviteTeacher(s, rs, null);
    }

    public static InviteTeacher parseInviteTeacher(InviteTeacher s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setItid(rs.getLong(su+"itid"));
        } catch (SQLException e) {
            s.setItid(null);
        }
        try {
            s.setTname(rs.getString(su+"tname"));
        } catch (SQLException e) {
            s.setTname(null);
        }
        try {
            s.setPhone(rs.getString(su+"phone"));
        } catch (SQLException e) {
            s.setPhone(null);
        }
        try {
            s.setGid(rs.getLong(su+"gid"));
        } catch (SQLException e) {
            s.setGid(null);
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            s.setState(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, InviteTeacher s) {
        par.put("itid",s.getItid());
        par.put("tname",s.getTname());
        par.put("phone",s.getPhone());
        par.put("gid",s.getGid());
        par.put("sid",s.getSid());
        par.put("cid",s.getCid());
        par.put("tid",s.getTid());
        par.put("state",s.getState());
        par.put("ctime",s.getCtime());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, InviteTeacher s) {
        args.put("itid",s.getItid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final InviteTeacher s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "itid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(InviteTeacher s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into invite_teacher(");
        int count = 0;
        if (null != s.getTname()) {
            sql.append("tname,");
            count++;
        }
        if (null !=s.getPhone()) {
            sql.append("phone,");
            count++;
        }
        if (null != s.getGid()) {
            sql.append("gid,");
            count++;
        }
        if (null != s.getSid()) {
            sql.append("sid,");
            count++;
        }
        if (null != s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != s.getTid()) {
            sql.append("tid,");
            count++;
        }
        if (null != s.getState()) {
            sql.append("state,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, InviteTeacher s)
        throws SQLException {
        int count = 1;
        if (null != s.getTname()) {
            ps.setString(count++,s.getTname());
        }
        if (null != s.getPhone()) {                                                 
            ps.setString(count++, s.getPhone());
        }
        if (null != s.getGid()) {
            ps.setLong(count++,s.getGid());
        }
        if (null != s.getSid()) {
            ps.setInt(count++,s.getSid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++,s.getCid());
        }
        if (null != s.getTid()) {
            ps.setLong(count++,s.getTid());
        }
        if (null != s.getState()) {
            ps.setShort(count++,s.getState());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }


    }

}
